package com.caixy.adminSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caixy.adminSystem.common.ErrorCode;
import com.caixy.adminSystem.exception.ThrowUtils;
import com.caixy.adminSystem.model.dto.fileinfo.AddFileInfoRequest;
import com.caixy.adminSystem.model.dto.fileinfo.DeleteFileInfoRequest;
import com.caixy.adminSystem.model.dto.fileinfo.SelectFileInfoRequest;
import com.caixy.adminSystem.model.dto.fileinfo.UpdateFileInfoRequest;
import com.caixy.adminSystem.model.entity.FileInfo;
import com.caixy.adminSystem.service.FileInfoService;
import com.caixy.adminSystem.mapper.FileInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author dio
* @description 针对表【tb_file_info(文件信息表)】的数据库操作Service实现
* @createDate 2024-06-06 16:56:43
*/
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo>
    implements FileInfoService {
    @Resource
    private FileInfoMapper fileInfoMapper;

    @Override
    public void SaveAddFileInfo(AddFileInfoRequest addFileInfoRequest) {
        FileInfo fileInfo = new FileInfo();
//        是否为空验证
        if (true) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(
                    addFileInfoRequest.getFilePublicName(),
                    addFileInfoRequest.getFileTags(),
                    addFileInfoRequest.getFileDescription(),
                    addFileInfoRequest.getFileSha256()
            ), ErrorCode.PARAMS_ERROR);
        }
        fileInfo.setUploaderId(addFileInfoRequest.getUploaderId());
        fileInfo.setFileCategoryId(addFileInfoRequest.getFileCategoryId());
        fileInfo.setFileTags(addFileInfoRequest.getFileTags());
        fileInfo.setFileTypeId(addFileInfoRequest.getFileTypeId());
        fileInfo.setFileDescription(addFileInfoRequest.getFileDescription());
        fileInfo.setFileSize(addFileInfoRequest.getFileSize());
        fileInfo.setFileSha256(addFileInfoRequest.getFileSha256());
        fileInfo.setFilePublicName(addFileInfoRequest.getFilePublicName());

        fileInfoMapper.insert(fileInfo);


    }

    @Override
    public void DeleteFileInfo(DeleteFileInfoRequest deleteFileInfoRequest) {
        FileInfo fileInfo = new FileInfo();

        if (deleteFileInfoRequest.getId() != null) {
            fileInfo.setId(deleteFileInfoRequest.getId());
            fileInfoMapper.deleteById(fileInfo.getId());
        }
    }

    @Override
    public void SaveUpdateFileInfo(UpdateFileInfoRequest updateFileInfoRequest) {
        FileInfo fileInfo = new FileInfo();
//        是否为空验证
        if (true) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(
                    updateFileInfoRequest.getFilePublicName(),
                    updateFileInfoRequest.getFileTags(),
                    updateFileInfoRequest.getFileDescription(),
                    updateFileInfoRequest.getFileSha256()
            ), ErrorCode.PARAMS_ERROR);
        }
        fileInfo.setId(updateFileInfoRequest.getId());
        fileInfo.setUploaderId(updateFileInfoRequest.getUploaderId());
        fileInfo.setFileCategoryId(updateFileInfoRequest.getFileCategoryId());
        fileInfo.setFileTags(updateFileInfoRequest.getFileTags());
        fileInfo.setFileTypeId(updateFileInfoRequest.getFileTypeId());
        fileInfo.setFileDescription(updateFileInfoRequest.getFileDescription());
        fileInfo.setFileSize(updateFileInfoRequest.getFileSize());
        fileInfo.setFileSha256(updateFileInfoRequest.getFileSha256());
        fileInfo.setFilePublicName(updateFileInfoRequest.getFilePublicName());

        // where = fileInfo.getId();
        fileInfoMapper.updateById(fileInfo);
    }

    @Override
    public void SelectFileInfo(SelectFileInfoRequest selectFileInfoRequest) {
        FileInfo fileInfo = new FileInfo();

        if (selectFileInfoRequest.getId() != null) {
            fileInfo.setId(selectFileInfoRequest.getId());
            fileInfoMapper.selectById(fileInfo.getId());
        }

    }

    @Override
    public Long CountFileInfoByTypeId(Long TypeId) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fileTypeId", TypeId);

        return this.fileInfoMapper.selectCount(queryWrapper);
    }

    @Override
    public Long CountFileImfoByCategoryId(Long CategoryId) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fileCategoryId", CategoryId);

        return this.fileInfoMapper.selectCount(queryWrapper);
    }


}